package com.sky.controller.admin;

import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.xmlbeans.impl.inst2xsd.RussianDollStrategy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/admin/setmeal")
@Api("套餐管理")
@RestController
@Slf4j
public class SetmealController {
    @Autowired
    private SetmealService setmealService;

    /**
     * 增加套餐信息
     *
     * @return
     */
    @PostMapping
    @ApiOperation("增加套餐")
    @CacheEvict(cacheNames = "setmealCache",key = "#setmealDTO.categoryId") //删除缓存  精确清理
    public Result addSetmeal(@RequestBody SetmealDTO setmealDTO) {
        setmealService.addSetmeal(setmealDTO);
        return Result.success();
    }

    /**
     * 套餐的分页查询
     *
     * @param setmealPageQueryDTO
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("套餐的分页查询")
    public Result<PageResult> page(SetmealPageQueryDTO setmealPageQueryDTO) {
        log.info("套餐的分页查询：{}", setmealPageQueryDTO);
        PageResult pageResult = setmealService.queryPage(setmealPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 删除套餐
     *
     * @param ids
     * @return
     */
    @DeleteMapping
    @ApiOperation("删除套餐")
    @CacheEvict(cacheNames = "setmealCache",allEntries = true) //删除缓存  所有数据
    public Result delSetmeal(@RequestParam List<Long> ids) {
        log.info("删除套餐相关数据:{}", ids);
        setmealService.delSetmeal(ids);
        return Result.success();
    }

    /**
     * 修改套餐的状态
     *
     * @param status
     * @param id
     * @return
     */
    @PostMapping("/status/{status}")
    @ApiOperation("修改套餐状态")
    @CacheEvict(cacheNames = "setmealCache",allEntries = true) //删除缓存  所有数据
    public Result editStatus(@PathVariable Integer status, Long id) {
        log.info("修改套餐的状态：{}", status);
        setmealService.editStatus(status, id);
        return Result.success();
    }

    @GetMapping("/{id}")
    @ApiOperation("根据id查询套餐")
    public Result<SetmealVO> getSetmealById(@PathVariable Long id) {
        log.info("根据ID查询套餐数据：{}", id);
        SetmealVO setmealVO = setmealService.getSetmealWithDish(id);
        return Result.success(setmealVO);
    }

    /**
     * 修改套餐数据
     *
     * @param setmealDTO
     * @return
     */
    @PutMapping
    @ApiOperation("修改套餐数据")
    @CacheEvict(cacheNames = "setmealCache",allEntries = true) //删除缓存  所有数据
    public Result editSetmeal(@RequestBody SetmealDTO setmealDTO) {
        setmealService.editSetmeal(setmealDTO);
        return Result.success();
    }
}
